package book;
import java.util.Arrays;

public class BookList {
    private Book[] books;
    private int num;

    public BookList(Book[] books, int num) {
        this.books = books;
        this.num = num;
    }

    public BookList() {
        this.books = new Book[0];
        this.num = 0;
    }
    public BookList(String str) {
        this.books = new Book[3];
        this.num = 3;
        this.books[0] = new Book(1,"测试书籍1", "测试使用", "xtc", 32);
        this.books[1] = new Book(2,"测试书籍2", "测试使用", "xtc", 43);
        this.books[2] = new Book(3,"测试书籍3", "测试使用", "xtc", 54);
    }
    public void addBookList(int num) {
        this.books = Arrays.copyOf(this.books, this.num);
    }

    public Book getBook(int index) {
        return this.books[index];
    }

    public void setNewBook(Book book) {
        this.addBookList(1);
        this.books[num++]  = book;
    }
    public void setBook(Book book, int index) {
        this.books[index] = book;
    }
    public void setBook(Book book, int index, int n) {
        this.books[index] = book;
        if(book != null) {
            books[index].setNum(books[index].getNum() + n);
        }
    }

    public Book numFind(int num) {
        int left = 0;
        int right = this.num-1;
        int mid;
        num--;
        while(left <= right) {
            mid = (left+right)/2;
            if(mid < num) {
                left = mid+1;
            }else if(mid > num) {
                right = mid-1;
            }else {
                return books[mid];
            }
        }
        return null;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }
}
